package com.dzl.goods;

import com.alibaba.fastjson.JSONObject;
import com.dzl.base.controller.BaseController;
import com.dzl.config.exception.CommonException;
import com.dzl.constant.CommonConstants;
import com.dzl.goods.pojo.WelfareGoodsCategory;
import com.dzl.goods.pojo.query.WelfareGoodsCategoryQuery;
import com.dzl.goods.pojo.query.WelfareGoodsManageQuery;
import com.dzl.goods.pojo.vo.WelfareGoodsCategoryVo;
import com.dzl.goods.pojo.vo.WelfareJicaiGoodsManageVo;
import com.dzl.goods.service.WelfareGoodsCategoryRelService;
import com.dzl.goods.service.WelfareGoodsCategoryService;
import com.dzl.goods.service.WelfareGoodsService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.PageUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 商品品类管理 - 控制层
 * @author WangJian
 * @date 2021/9/18 0018
 */
@RestController
@RequestMapping("/welfareGoodsCategory")
public class WelfareGoodsCategoryController extends BaseController {

    @Autowired
    private WelfareGoodsCategoryService service;
    @Autowired
    private WelfareGoodsCategoryRelService welfareGoodsCategoryRelService;
    @Autowired
    private WelfareGoodsService welfareGoodsService;

    /**
     * 分页查询商品品类
     * @date 2021/9/18 0018 10:05
     * @author wangjian
     * @param query 分页查询条件
     * @return com.dzl.util.PageUtils<com.dzl.goods.pojo.WelfareGoodsCategory>
     */
    @GetMapping("/selectCategoryListData")
    public PageUtils<WelfareGoodsCategoryVo> selectCategoryListData(WelfareGoodsCategoryQuery query){
        PageInfo<WelfareGoodsCategoryVo> page = service.selectCategoryListPage(query);
        return new PageUtils<> (page);
    }

    /**
     * 品类关联商品数据列表查询
     * @param query 查询实体类
     * @author liuguanjun
     * @date 2021-12-23 11:15:38
     **/
    @GetMapping("getGoodsListManage")
    public PageUtils<WelfareJicaiGoodsManageVo> getGoodsListManage(WelfareGoodsManageQuery query) {
        query.setIsJicai(CommonConstants.YES);
        if(EmptyUtils.isEmpty(query.getCategoryId())){
            throw new CommonException("品类id不能传空");
        }
        if(query.getIsChecked() == null){
            throw new CommonException("isChecked字段不能传空");
        }
        PageInfo<WelfareJicaiGoodsManageVo> pageInfo = welfareGoodsService.getWelfareJicaiGoodsListPage(query);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 商品品类下拉框（查询全部）
     * @date 2021/9/18 0018 10:44
     * @author wangjian
     * @return com.dzl.util.JsonResult<java.util.List<com.dzl.goods.pojo.WelfareGoodsCategory>>
     */
    @GetMapping("/selectCategoryList")
    public JsonResult<List<WelfareGoodsCategoryVo>> selectCategoryList(){
        return JsonResult.success(service.selectCategoryListData());
    }

    /**
     * 根据ID查品类（编辑回显）
     * @date 2021/9/18 0018 10:46
     * @author wangjian
     * @param id 品类ID
     * @return com.dzl.util.JsonResult<com.dzl.goods.pojo.WelfareGoodsCategory>
     */
    @GetMapping("/selectCategoryById")
    public JsonResult<WelfareGoodsCategory> selectCategoryById(Integer id){
        return JsonResult.success(service.selectByPrimaryKey(id));
    }

    /**
     * 删除品类
     * @date 2021/9/18 0018 10:12
     * @author wangjian
     * @param id 品类ID
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @DeleteMapping("/deleteCategory")
    public JsonResult<Integer> deleteCategory(Integer id){
        return JsonResult.success(service.deleteCategory(id));
    }

    /**
     * 删除品类关联集采商品
     * @param categoryId 品类ID
     * @param goodsId 商品ID
     * @date 2022/01/06 09:35
     * @author liuguanjun
     */
    @DeleteMapping("/deleteCategoryRelGoods")
    public JsonResult<Integer> deleteCategoryRelGoods(Integer categoryId, Integer goodsId){
        if(EmptyUtils.isEmpty(goodsId)){
            return JsonResult.paramErrWithMsg("请选择商品");
        }
        if(EmptyUtils.isEmpty(categoryId)){
            return JsonResult.paramErrWithMsg("品类id不能传空");
        }
        return JsonResult.success(welfareGoodsCategoryRelService.deleteCategoryRelGoods(categoryId, goodsId));
    }


    /**
     * 保存品类
     * @date 2021/9/18 0018 10:16
     * @author wangjian
     * @param welfareGoodsCategory 品类信息
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @PostMapping("/saveCategory")
    public JsonResult<Integer> saveCategory(@RequestBody WelfareGoodsCategory welfareGoodsCategory){
        welfareGoodsCategory.setStatus(1);
        if (EmptyUtils.isEmpty(welfareGoodsCategory.getCategoryName())){
            return JsonResult.failWithMsg("品类名称不能为空");
        }
        WelfareGoodsCategory category = new WelfareGoodsCategory();
        category.setStatus(1);
        category.setCategoryName(welfareGoodsCategory.getCategoryName());
        List<WelfareGoodsCategory> list = service.select(category);
        if (EmptyUtils.isEmpty(welfareGoodsCategory.getId())){
            //添加操作
            if (EmptyUtils.isNotEmpty(list)){
                return JsonResult.failWithMsg("该品类已存在");
            }
            welfareGoodsCategory.setCreateTime(new Date());
            service.insertSelective(welfareGoodsCategory);
        }else {
            //编辑操作
            WelfareGoodsCategory select = service.selectByPrimaryKey(welfareGoodsCategory.getId());
            if ((!welfareGoodsCategory.getCategoryName().equals(select.getCategoryName())) && EmptyUtils.isNotEmpty(list)){
                return JsonResult.failWithMsg("该品类已存在");
            }
            welfareGoodsCategory.setUpdateTime(new Date());
            service.updateByPrimaryKeySelective(welfareGoodsCategory);
        }
        return JsonResult.success();
    }

    /**
     * 选择商品，保存
     * @param param goodsIds 选中的商品
     * @param param categoryId 品类id
     * @author liuguanjun
     * @date 2021/12/20 15:20
     */
    @PostMapping("/saveGoodsCategoryRel")
    public JsonResult<Integer> saveGoodsCategoryRel(@RequestBody JSONObject param){
        String goodsIds = param.getString("goodsIds");
        Integer categoryId = param.getInteger("categoryId");
        if(EmptyUtils.isEmpty(goodsIds)){
            return JsonResult.paramErrWithMsg("请选择商品");
        }
        if(EmptyUtils.isEmpty(categoryId)){
            return JsonResult.paramErrWithMsg("品类id不能传空");
        }
        return JsonResult.success(welfareGoodsCategoryRelService.saveGoodsCategoryRel(categoryId,goodsIds));
    }
}
